package myJs.user.soft.fileSelect

import myJs.Implicits.JArJDS
import myJs.myPkg.jquery.$
import myJs.tool.Tool
import myJs.user.soft._
import myJs.myPkg.bootstrap.Bootstrap.default._
import myJs.tool.Pojo.FilterContentData
import org.scalajs.dom.{Element, Event, document}

import scala.scalajs.js
import myJs.Implicits._
import myJs.Utils._
import scala.scalajs.js.Dynamic.{global=>g}
import myJs.myPkg.ColumnOptions
import shared.VarTool
import scalatags.Text.all._
import myJs.tool.Tool._

/**
 * Created by yz on 19/10/2020
 */
object FileModal {

  val fileModalId = "fileModal"

  def init = {
    BiomSelectTab.init
    initTable
    registerEvent
  }

  def registerEvent = {
    tableFilterEvent
    $(s"#${fileModalId} .myMainUl a[data-toggle='tab']").on("shown.bs.tab", (y: Element, e: Event) => {
      refreshTable()
    })
  }

  def initTable = {
    MetSelectTab.initTable
    GroupSelectTab.initTable
    VarInfoSelectTab.initTable
    MissionResultSelectTab.initTable
    ExampleSelectTab.initTable
  }

  def getCurTabId = {
    $(s"#${fileModalId} .myMainUl li.active a").attr("href").get.replaceAll("^#", "")
  }

  def refreshTable(f: JArJDS => js.Any = (myData) => ()) = {
    getCurTabId match {
      case MetSelectTab.tabId => MetSelectTab.refreshTable()
      case VarInfoSelectTab.tabId => VarInfoSelectTab.refreshTable()
      case MissionResultSelectTab.tabId => MissionResultSelectTab.refreshTable()
      case GroupSelectTab.tabId => GroupSelectTab.refreshTable()
      case ExampleSelectTab.tabId => ExampleSelectTab.refreshTable()
      case BiomSelectTab.tabId => BiomSelectTab.refreshTable()
    }
  }

  def fileSelectShow(lis: Array[String]) = {
    $(s"#${fileModalId} .myMainUl li").hide().removeClass("active")
    lis.foreach { liStr =>
      $(s"#${fileModalId} a[href='#${liStr}']").parent("li").show()
    }
    val firstIsExample = lis.head == "example"
    val selectTab = if (firstIsExample) {
      lis(1)
    } else lis.head
    if (firstIsExample) {
      $(s"#${fileModalId} a[href='#example']").parent("li").insertAfter($(s"a[href='#${selectTab}']").parent("li"))
    }
    $(s"#${fileModalId} a[href='#${selectTab}']").parent("li").addClass("active")

    $(s"#${fileModalId} #myTabContent .tab-pane").removeClass("active in")
    $(s"#${fileModalId} #myTabContent #${selectTab}").addClass("active in")
    refreshTable()
    $(s"#${fileModalId}").modal("show")
  }

  def tableFilterEvent = {
    Tool.clickHide
    $(s"#${fileModalId} .myOperate").on("click", (y: Element) => {
      Tool.filterContentShowInModal(y, fileModalId)
    })
    $(document.body).on("click", s"#${fileModalId} .mySortLi", "", (y: Element) => {
      val parentId = s"${fileModalId} #${Tool.getCurTabId($(s"#${fileModalId}"))}"
      Tool.execSort(y, parentId, () => {
        refreshTable()
      })
    })
    $(document.body).on("click", s"#${fileModalId} .myConfirm", "", (y: Element) => {
      val parentId = s"${fileModalId} #${Tool.getCurTabId($(s"#${fileModalId}"))}"
      Tool.confirmFilter(y, parentId, () => {
        refreshTable()
      })
    })
    $(document.body).on("click", s"#${fileModalId} .myHide", "", (y: Element) => {
      val parentId = s"${fileModalId} #${Tool.getCurTabId($(s"#${fileModalId}"))}"
      Tool.hideFilterContent(y)
    })
    $(document.body).on("click", s"#${fileModalId} .myRemove", "", (y: Element) => {
      val parentId = s"${fileModalId} #${Tool.getCurTabId($(s"#${fileModalId}"))}"
      Tool.removeCondition(y, parentId, () => {
        refreshTable()
      })
    })
    $(document.body).on("click", s"#${fileModalId} .myRemoveSort", "", (y: Element) => {
      val parentId = s"${fileModalId} #${Tool.getCurTabId($(s"#${fileModalId}"))}"
      Tool.removeSort(y, parentId, () => {
        refreshTable()
      })
    })
  }

  def initFilterContent(tabId: String = "") = {
    val textSortDatas = List(
      FilterContentData("fileName", messages("fileName")),
      FilterContentData("comment", messages("comment")),
    )
    val sortDatas = List(
      FilterContentData("size", messages("size"), List("", "0KB-100KB", "100KB-1MB", "1MB-10MB")),
      FilterContentData("tags", messages("tag"), "" :: Tool.tagMap.valueList)
    )
    val timeSortDatas = List(
      FilterContentData("uploadTime", messages("uploadModifyTime"))
    )
    Tool.initFilterContent(textSortDatas = textSortDatas, sortDatas = sortDatas, timeSortDatas = timeSortDatas,
      parentId = s"${tabId}")
  }

  lazy val operateColumn = js.Array(
    ColumnOptions.field("operate").title(messages("operations")).formatter(operateFmt).filterControl("clear")
  )

  def operateFmt: js.Function = {
    (v: js.Any, row: js.Dictionary[js.Any]) =>
      val kind = row("kind").toString
      val folder = VarTool.dataKindMap(kind).showDirName

      val selectStr = button(`class` := "btn  green btn-xs", title := messages("select"), `type` := "button",
        `class` := "myFileSelectButton", dataFolder := s"${folder}", dataFileName := s"${row("fileName").toString}",
        messages("select")
      )

      val rs = List(selectStr)
      rs.mkString("&nbsp;")
  }

  def tbFmt(columnName: String): js.Function = (v: js.Any, row: js.Dictionary[js.Any]) => columnName match {
    case "fileName" =>
      Tool.fileNameA(v, row)
    case "size" => Tool.getFileSize(v)
    case "tags" => Tool.tagMap.getOrElse(v.toString, v.toString)
    case _ => v
  }


}
